OPC Studio User's Guide and Reference
Examples - OPC UA GDS and CM - Query for servers
View with Navigation Tools

.NET

// Shows how to find server applications that meet the specified filters, using the global discovery client.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

using System;
using OpcLabs.EasyOpc.UA.Discovery;
using OpcLabs.EasyOpc.UA.Gds;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples.Gds._EasyUAGlobalDiscoveryClient
{
    class QueryServers
    {
        public static void Main1()
        {
            // Instantiate the global discovery client object
            var globalDiscoveryClient = new EasyUAGlobalDiscoveryClient();

            // Find all servers registered in the GDS.
            UAServerOnNetwork[] serverOnNetworkArray;
            try
            {
                globalDiscoveryClient.QueryServers(
                    gdsEndpointDescriptor:"opc.tcp://opcua.demo-this.com:58810/GlobalDiscoveryServer",
                    startingRecordId:0,
                    maximumRecordsToReturn:0,
                    applicationName:"",
                    applicationUriString:"",
                    productUriString:"",
                    serverCapabilities:new string[0],
                    lastCounterResetTime:out _,
                    serverOnNetworkArray:out serverOnNetworkArray);
            }
            catch (UAException uaException)
            {
                Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message);
                return;
            }

            // Display results
            foreach (UAServerOnNetwork serverOnNetwork in serverOnNetworkArray)
            {
                Console.WriteLine();
                Console.WriteLine("Server name: {0}", serverOnNetwork.ServerName);
                Console.WriteLine("Discovery URL string: {0}", serverOnNetwork.DiscoveryUrlString);
                Console.WriteLine("Server capabilities: {0}", serverOnNetwork.ServerCapabilities);
            }
        }
    }
}

COM

// Shows how to find server applications that meet the specified filters, using the global discovery client.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in Object Pascal (Delphi) on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-OP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.

class procedure QueryServers.Main;
var
  ApplicationName: WideString;
  ApplicationUriString: WideString;
  GlobalDiscoveryClient: OpcLabs_EasyOpcUA_TLB._EasyUAGlobalDiscoveryClient;
  GdsEndpointDescriptor: _UAEndpointDescriptor;
  I: integer;
  LastCounterResetTime: TDateTime;
  MaximumRecordsToReturn: Integer;
  ProductUriString: WideString;
  ServerCapabilities: array of string;
  ServerOnNetwork: _UAServerOnNetwork;
  ServerOnNetworkArray: OleVariant;
  StartingRecordId: Integer;
begin
  // Define which GDS we will work with.
  GdsEndpointDescriptor := CoUAEndpointDescriptor.Create;
  GdsEndpointDescriptor.UrlString := 'opc.tcp://opcua.demo-this.com:58810/GlobalDiscoveryServer';

  // Instantiate the global discovery client object
  GlobalDiscoveryClient := CoEasyUAGlobalDiscoveryClient.Create;

  // Find all servers registered in the GDS.
  StartingRecordId := 0;
  MaximumRecordsToReturn := 0;
  ApplicationName := '';
  ApplicationUriString := '';
  ProductUriString := '';
  try
    GlobalDiscoveryClient.QueryServers(
      GdsEndpointDescriptor,
      StartingRecordId,
      MaximumRecordsToReturn,
      ApplicationName,
      ApplicationUriString,
      ProductUriString,
      ServerCapabilities,
      LastCounterResetTime,
      ServerOnNetworkArray);
  except
    on E: EOleException do
    begin
      WriteLn(Format('*** Failure: %s', [E.GetBaseException.Message]));
    end;
  end;

  // Display results
  for I := VarArrayLowBound(ServerOnNetworkArray,1) to VarArrayHighBound(ServerOnNetworkArray,1) do
  begin
    ServerOnNetwork := IUnknown(ServerOnNetworkArray[I]) as _UAServerOnNetwork;
    WriteLn;
    WriteLn('Server name: ', ServerOnNetwork.ServerName);
    WriteLn('Discovery URL string: ', ServerOnNetwork.DiscoveryUrlString);
    WriteLn('Server capabilities: ', ServerOnNetwork.ServerCapabilities.ToString);
  end;

end;

Python

# Shows how to find server applications that meet the specified filters, using the global discovery client.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.Discovery import *
from OpcLabs.EasyOpc.UA.Gds import *
from OpcLabs.EasyOpc.UA.OperationModel import *


# Define which GDS we will work with.
gdsEndpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:58810/GlobalDiscoveryServer')

# Instantiate the global discovery client object.
globalDiscoveryClient = EasyUAGlobalDiscoveryClient()

# Find all servers registered in the GDS.
try:
    _, _, serverOnNetworkArray = globalDiscoveryClient.QueryServers(
        gdsEndpointDescriptor,
        0,  # startingRecordId
        0,  # maximumRecordsToReturn
        '', # applicationName
        '', # applicationUriString
        '', # productUriString
        Array.Empty[String](),  # serverCapabilities
        DateTime(), # out lastCounterResetTime
        Array.Empty[UAServerOnNetwork]()) # out serverOnNetworkArray
except UAException as uaException:
    print('*** Failure: ' + uaException.GetBaseException().Message)
    exit()

# Display results.
for serverOnNetwork in serverOnNetworkArray:
    print()
    print('Server name: ', serverOnNetwork.ServerName, sep='')
    print('Discovery URL string: ', serverOnNetwork.DiscoveryUrlString, sep='')
    print('Server capabilities: ', serverOnNetwork.ServerCapabilities, sep='')

print()
print('Finished.')
See Also

Concepts

Fundamentals